package org.desktop.diskutils.business.categories.impl;

import java.util.Locale;

import org.desktop.diskutils.business.categories.IFileCategoryEntity;

public abstract class FileCategoryEntity implements IFileCategoryEntity {

	private Integer color;
	private String name;
	private FileCategory parent;

	public FileCategoryEntity() {
		this(null, "");
	}

	public FileCategoryEntity(final Integer color) {
		this(color, "");
	}

	public FileCategoryEntity(final String name) {
		this(null, name);
	}

	public FileCategoryEntity(final Integer color, final String name) {
		this.color = color;
		setName(name);
	}

	@Override
	public Integer getColor() {
		return color;
	}

	public void setColor(final int color) {
		this.color = color;
	}

	@Override
	public String getName() {
		return name;
	}

	public void setName(final String name) {
		if (this.name != null) {
			throw new IllegalArgumentException("Can not change a category's name");
		}
		this.name = name;
	}

	@Override
	public FileCategory getParent() {
		return parent;
	}

	public void setParent(final FileCategory parent) {
		this.parent = parent;
		if (!parent.getChildren().contains(this)) {
			parent.addChild(this);
		}
	}
	
	protected static String normalizeName(final String extension) {
		return extension == null ? null : extension.toLowerCase(Locale.US);
	}
}
